<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\GrGoodsType;
use App\Models\GoodsTypeMold;
use App\Models\Type;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;

class GrGoodsTypeController extends AdminController
{

    public function __construct(){
        $type = \App\Models\GrGoodsType::all()->toArray();
        $typeInfo=[];
        $typeInfo[0]='顶级';
        foreach ($type as $val){
            $typeInfo[$val['goods_type_id']]=$val['goods_type_name'];
        }
        $mold=GoodsTypeMold::all()->toArray();
        $moldInfo=[];
        foreach ($mold as $val){
            $moldInfo[$val['goods_mold_id']]=$val['goods_mold_name'];
        }
        $this->type = $typeInfo;
        $this->mold = $moldInfo;
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GrGoodsType(['mold']), function (Grid $grid) {
            $grid->column('goods_type_id','商品分类ID')->sortable();
            // 开启树状结构
            $grid->column('goods_type_name')->tree();
            $grid->column('mold.goods_mold_name','类型');
            $grid->column('created_at');
            $grid->column('status','状态')->switch('', true);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('status','状态')->select(['1' => '开启','0'=>'关闭'])->width(3);
                $filter->expand();
                $filter->panel();

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GrGoodsType(), function (Show $show) {
            $show->field('goods_type_id');
            $show->field('pid');
            $show->field('goods_type_name');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GrGoodsType(), function (Form $form) {

            $form->display('goods_type_id');
            $form->select('pid')->options($this->type);
            $form->select('goods_mold_id','类型')->options($this->mold);
            $form->text('goods_type_name');
            $form->switch('status','状态')
                ->customFormat(function ($v) {
                    return $v == '1' ? 1 : 0;
                })
                ->saving(function ($v) {
                    return $v ? '1' : '0';
                });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

}
